package com.code2roc.jarmanage.util;

import org.yaml.snakeyaml.Yaml;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class ConfigUtil {
    public static String getConfigValue(String configName) {
        String value = getCustomConfigValue(configName);
        if (StringUtil.isEmpty(value)) {
            value = getDefaultConfigValue(configName);
        }
        return value;
    }

    private static String getDefaultConfigValue(String configName) {
        String configValue = "";
        try {
            Yaml yaml = new Yaml();
            InputStream resourceAsStream = new FileInputStream(new File("resources" + File.separator + "application.yml"));
            Map obj = yaml.load(resourceAsStream);
            Map<String, Object> param = (Map) obj.get("jarmanage");
            configValue = ConvertUtil.convert2String(param.get(configName));
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return configValue;
    }

    private static String getCustomConfigValue(String configName) {
        String configValue = "";
        try {
            String defaultFilePath = "resources" + File.separator + "application.yml";
            String customFilePath = "resources" + File.separator + "application-custom.yml";
            if (!new File(customFilePath).exists()) {
                FileUtil.copyFile(defaultFilePath, customFilePath);
            }
            Yaml yaml = new Yaml();
            InputStream resourceAsStream = new FileInputStream(new File(customFilePath));
            Map obj = yaml.load(resourceAsStream);
            Map<String, Object> param = (Map) obj.get("jarmanage");
            configValue = ConvertUtil.convert2String(param.get(configName));
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return configValue;
    }

    public static String getConfigValue(String name, String configName) {
        String value = getCustomConfigValue(name,configName);
        if (StringUtil.isEmpty(value)) {
            value = getDefaultConfigValue(name,configName);
        }
        return value;
    }

    public static String getDefaultConfigValue(String name, String configName) {
        String configValue = "";
        try {
            Yaml yaml = new Yaml();
            InputStream resourceAsStream = new FileInputStream(new File("resources" + File.separator + "application.yml"));
            Map obj = yaml.load(resourceAsStream);
            Map<String, Object> param = (Map) obj.get(name);
            configValue = ConvertUtil.convert2String(param.get(configName));
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return configValue;
    }

    public static String getCustomConfigValue(String name, String configName) {
        String configValue = "";
        try {
            String defaultFilePath = "resources" + File.separator + "application.yml";
            String customFilePath = "resources" + File.separator + "application-custom.yml";
            if (!new File(customFilePath).exists()) {
                FileUtil.copyFile(defaultFilePath, customFilePath);
            }
            Yaml yaml = new Yaml();
            InputStream resourceAsStream = new FileInputStream(new File(customFilePath));
            Map obj = yaml.load(resourceAsStream);
            Map<String, Object> param = (Map) obj.get(name);
            configValue = ConvertUtil.convert2String(param.get(configName));
        } catch (Exception e) {
            LogUtil.error(e);
        }
        return configValue;
    }
}
